package com.shoulder.core.utils;

import com.google.common.base.Strings;

public class CharacterUtils {

    /**
     * 字符串首字母转大写
     *
     * @param input 待转的字符串
     * @return 新的字符串
     */
    public static String upperFirst(String input) {
        if (Strings.isNullOrEmpty(input)) {
            return input;
        }

        char firstChar = Character.toUpperCase(input.charAt(0));
        String rest = input.substring(1);
        return firstChar + rest;
    }

    /**
     * 字符串首字母转小写
     *
     * @param input 待转的字符串
     * @return 新的字符串
     */
    public static String lowerFirst(String input) {
        if (Strings.isNullOrEmpty(input)) {
            return input;
        }

        char firstChar = Character.toLowerCase(input.charAt(0));
        String rest = input.substring(1);
        return firstChar + rest;
    }

    /**
     * 下划线转驼峰
     *
     * @param input 待转的字符串
     * @return 新的字符串
     */
    public static String underscoreToCamelCase(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        StringBuilder result = new StringBuilder();
        boolean capitalizeNext = false;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '_') {
                capitalizeNext = true;
            } else if (capitalizeNext) {
                result.append(Character.toUpperCase(c));
                capitalizeNext = false;
            } else {
                result.append(Character.toLowerCase(c));
            }
        }
        return result.toString();
    }

}
